import {messages} from "share/common";
import React from 'react';
import {connect} from 'react-redux';
import { Form, Card, Button, Input, message } from 'antd';
import airlineConfigService from './airline-config.service';

class AirlineConfigPolicy extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      travelStandard: '',
      intTravelStandard: ''
    };
  }
  componentDidMount() {
    this.props.form.validateFields();
    const { companyOID } = this.props;
    airlineConfigService.getConfig(companyOID)('travel').then((res) => {
      if (res.data.code !== 200) return;
      this.props.form.setFieldsValue({
        domestic: res.data.body.travelStandard,
        international: res.data.body.intTravelStandard
      });
    });
  }
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (err) return;
      const { domestic, international } = values;
      if (!domestic && !international) return;
      this.setState({loading: true});
      const data = {};
      if (domestic) (data.travelStandard = domestic);
      if (international) (data.intTravelStandard = international);
      const { companyOID } = this.props;
      airlineConfigService.updateCompanyConfig(companyOID)(data).then((res) => {
        if (res.data.code !== 200) return Promise.reject();
        return message.success(messages('common.operate.success')/*操作成功*/);
      }).catch(() => {
        message.error(messages('common.operate.filed')/*操作失败*/);
      }).finally(() => { this.setState({loading: false}); });
    });
  }
  render = () => {
    const { getFieldDecorator, getFieldsError, getFieldError } = this.props.form;
    return (<div className="airline-config-container">
      <Form onSubmit={this.handleSubmit}>
        <Card
          type="inner"
          title={messages('supplier.key251')/*国内机票政策*/}
        >
          <Form.Item>
            {getFieldDecorator('domestic')(
              <Input.TextArea rows={5} placeholder={messages('common.please.input')/*请输入*/}/>
            )}
          </Form.Item>
        </Card>
        <Card
          type="inner"
          title={messages('supplier.key252')/*国际机票政策*/}
        >
          <Form.Item>
            {getFieldDecorator('international')(
              <Input.TextArea rows={5} placeholder={messages('common.please.input')/*请输入*/} />
            )}
          </Form.Item>
        </Card>
        <Form.Item>
          <Button
            type="primary"
            htmlType="submit"
            loading={this.state.loading}
          >{messages('supplier.key250')/*保存配置*/}</Button>
        </Form.Item>
      </Form>
    </div>);
  }
}

function mapStateToProps() {
  return {};
}

AirlineConfigPolicy.propTypes = {
  companyOID: React.PropTypes.string
};

const WrappedAirlineConfigPolicy = Form.create()(AirlineConfigPolicy);

export default connect(mapStateToProps)(WrappedAirlineConfigPolicy);
